#cython: boundscheck=False

import numpy as np
cimport numpy as np
cimport cython

INT32 = np.int32
ctypedef np.int32_t INT32_t
FLOAT32 = np.float32
ctypedef np.float32_t FLOAT32_t

cdef extern from "math.h":
    float sqrt(float)


def pt_line_dist_fast(np.ndarray[FLOAT32_t,ndim=1] pt,
                      np.ndarray[FLOAT32_t,ndim=2] xy_arr):
    
    cdef float a0 = pt[0]-xy_arr[0,0]
    cdef float a1 = pt[1]-xy_arr[0,1]
    cdef float b0 = xy_arr[0,0]-xy_arr[xy_arr.shape[0]-1,0]
    cdef float b1 = xy_arr[0,1]-xy_arr[xy_arr.shape[0]-1,1]
    cdef float sqrb = b0**2+b1**2
    cdef float dot = a0*b0+a1*b1
    cdef float c0 = a0-b0*dot/sqrb
    cdef float c1 = a1-b1*dot/sqrb
    return sqrt(c0**2+c1**2)
